%%游戏中心
-module(fun_game_center).
-include("common.hrl").

-export([do_init/0,do_close/0,do_call/1,do_info/1,do_time/1,do_msg/1,reported_ply_num/0]). 

%%初始化
do_init()-> 
	Ptm = lib_protocol:init_pt(),
	Ptm1 = regist_pt(Ptm),
	put(ptm,Ptm1),
	xtimer:start_timer(1800*1000,{?MODULE, reported_ply_num}),
	ok.

do_close() -> ok.

do_time(_Time) -> 1000.

do_call(_Msg) ->?debug("!!!!!!!!!!~p",[_Msg]), ok.


do_info({timeout, _TimerRef, CallBackInfo}) ->
	case CallBackInfo of
		{Module, Function} ->
			try
				Module:Function()
			catch E:R ->?log_error("timeout error,data=~p,E=~p,R=~p,stack=~p",[CallBackInfo,E,R,erlang:get_stacktrace()])
			end;
		{Module, Function, Args} ->
			try
				Module:Function(Args)
			catch E:R ->?log_error("timeout error,data=~p,E=~p,R=~p,stack=~p",[CallBackInfo,E,R,erlang:get_stacktrace()])
			end;
		_ ->
			?log_warning("unknown timer callback,CallbackInfo=~p", [CallBackInfo])
	end;
do_info(_Info) -> ok.

%%玩家进入初始化
do_msg({let_it_in,Aid,Sid,Ip,LogID,Seq}) ->
	?debug("let_it_in,{Aid,Sid,LogID,Seq} = ~p",[{Aid,Sid,LogID,Seq}]),
	gen_server:cast(Sid,{set_val, Aid, 0, 1}),
	
	case db:dirty_get(account, Aid) of
		[#account{name = Name,head_img = Head,card = Card,sex = Sex,last_logout_time = LastLogoutTime} |_] ->
			Pt = pt_info_a002:new(),
			Pt1 = Pt#pt_info{acc_id=Aid,name=util:to_list(Name),head=util:to_list(Head),card=Card,sex=Sex,
							 ip=util:ip_to_string(Ip),recharge_msg = data_sys:get_data(card_msg)},
			?send(Sid,pt_info_a002:to_binary(Pt1,Seq)),		
			group_info:req_get_group_room_list(Aid, Sid, Seq),
			case db:dirty_get(ply, Aid,#ply.aid) of
				[#ply{sid = Sid}|_] -> gen_server:cast(Sid, {discon, "relogin"});
				_ -> skip
			end,			
			db:dirty_put(#ply{sid = Sid,name = util:to_list(Name),aid = Aid,room_id =0,log_id=LogID}),		
			if
				LastLogoutTime == 0 andalso Card == 0 ->
					fun_player:add_card(Sid, Aid, ?START_CARD),
					?error_report(Sid,new_player_add_card);
				true -> skip
			end,					
			case db:dirty_get(off_ply, Aid) of
				[#off_ply{room_id = RoomID}] ->
					?debug("off_ply,{Aid,RoomID} = ~p",[{Aid,RoomID}]),
					Ptc = pt_action_d001:new(),
					Ptc1 = Ptc#pt_action{action = ?ACTION_HAS_ROOM},
					?send(Sid,pt_action_d001:to_binary(Ptc1)),				
					db:dirty_del(off_ply, Aid),
					fun_room_center:req_reenter_room(Sid,Aid,RoomID);
				_ -> skip
			end;
		_ -> skip
	end,
	ok;
%%创建房间
do_msg({created_room,Data}) ->
	fun_room_center:created_room(Data);
%%房间开始游戏
do_msg({room_start_game,Data}) ->
	fun_room_center:room_start_game(Data);
%%解散房间
do_msg({destroyed_room,Data}) ->
	fun_room_center:destroyed_room(Data);
%%玩家 房间id更新
do_msg({in_room,Sid,RoomID,_Aid}) ->
	case db:dirty_get(ply, Sid) of
		[Ply | _] ->
			db:dirty_put(Ply#ply{room_id=RoomID});
		_ -> skip
	end;
%%离开房间
do_msg({leave_room,Sid,_Aid}) ->
	case db:dirty_get(ply, Sid) of
		[Ply | _] ->
			db:dirty_put(Ply#ply{room_id=0});
		_ -> skip
	end;
%%玩家掉线
do_msg({close_tcp,Sid,Aid}) ->
	?debug("close_tcp,{Aid}=~p",[{Aid}]),
	case db:dirty_get(ply, Sid) of
		[#ply{log_id=LogID,room_id = RoomID} | _] ->
			?debug("close_tcp,{Aid,RoomID}=~p",[{Aid,RoomID}]),
			case db:dirty_get(acc_login_log, LogID) of
				[Log|_] ->
					db:dirty_put(Log#acc_login_log{logout_time = util:unixtime()});
				_ -> skip
			end,			
			fun_room_center:send_to_room(RoomID,{close_tcp,Sid,Aid}),
			case db:dirty_get(room, RoomID) of
				[#room{room_status = ?ROOM_GAME} | _] -> 
					?debug("game,aid off,{Aid,RoomID}=~p",[{Aid,RoomID}]),
					db:dirty_put(#off_ply{aid = Aid,room_id=RoomID});
				[#room{owner = Aid} | _] -> 
					?debug("owner,aid off,{Aid,RoomID}=~p",[{Aid,RoomID}]),
					db:dirty_put(#off_ply{aid = Aid,room_id=RoomID});
				_ -> skip
			end,			
			db:dirty_del(ply, Sid);
		_ -> skip
	end;
%%协议发送
do_msg({recv, Sid, Data}) ->
	case lib_protocol:fill_pt(Data, get(ptm)) of
		{ok,Name,Seq,Pt} ->
			process_pt(Name,Seq,Pt,Sid);
		R -> ?log_error("recv fill_pt error R=~p,Data = ~p",[R,Data])
	end;
%%更新玩家战绩
do_msg({update_sg_fighting_record,Room_owner,ZhuangAid,Roomid,Roomtype,Room_Mode,Room_score,Game_times,Fighting_record,Jiesuan_score_list}) ->
	san_gong_fight_record:update_sg_fighting_record(Room_owner,ZhuangAid,Roomid,Roomtype,Room_Mode,Room_score,Game_times,Fighting_record,Jiesuan_score_list);

do_msg(_Msg) -> ok.

get_pt_list()->
	[
	 pt_action_d001,
	 pt_action_int_d002,
	 pt_action_string_d004,
	 pt_create_csmj_d009,
	 pt_create_zzmj_d008,
	 pt_create_jd_sg_d200,
	 pt_create_jhsg_d202,
	 pt_create_swsg_d201
	].

regist_pt(Ptm) ->
	List = get_pt_list(),
	Fun = fun(Pt,Ptm) -> lib_protocol:regist_pt(Ptm, Pt) end,
	lists:foldl(Fun, Ptm, List).

process_pt(pt_action_string_d004,Seq,Pt,Sid) -> 
	?debug("pt_action_string_d004 Pt = ~p,Sid = ~p",[Pt,Sid]),
	Action = Pt#pt_action_string.action,
	case db:dirty_get(ply, Sid) of
		[#ply{aid = Aid}] ->
			case Action of
				?ACTION_JOIN_ROOM ->  fun_room_center:req_join_room(Sid,Aid,Pt#pt_action_string.data,Seq);
				?ACTION_JOIN_SG_ROOM ->fun_room_center:req_join_sg_room(Sid,Aid,Pt#pt_action_string.data,Seq);
				_ -> ?error_report(Sid,can_not_join)
			end;
		_ -> skip
	end;

process_pt(pt_action_d001,Seq,Pt,Sid) -> 
	?debug("pt_action_d001 Pt = ~p,Sid = ~p",[Pt,Sid]),
	Action = Pt#pt_action.action,
	case db:dirty_get(ply, Sid) of
		[#ply{aid = Aid}] ->
			case Action of
		         ?ACTION_SG_FIGHT_RECORD -> san_gong_fight_record:req_get_fightting_record_info(Sid,Aid);
				?ACTION_GET_FRIEND_LIST->friend_info:req_select_friend_room(Aid, Sid, Seq);
				_ -> skip
			end;
		_ -> skip
	end;

process_pt(pt_action_int_d002,Seq,Pt,Sid) -> 
	?debug("pt_action_int_d002 Pt = ~p,Sid = ~p",[Pt,Sid]),
	Action = Pt#pt_action_int.action,
	case db:dirty_get(ply, Sid) of
		[#ply{aid = Aid}] ->
			case Action of
				?ACTION_SG_EVERY_GAMES_FIGHTING ->san_gong_fight_record:req_get_every_times_info(Sid,Aid,Pt#pt_action_int.data);
				?ACTION_ADD_FRIEND->friend_info:req_add_friend(Aid, Pt#pt_action_int.data, Sid, Seq);
				?ACTION_DEL_FRIEND->friend_info:req_del_friend(Aid, Pt#pt_action_int.data, Sid, Seq);
				_ -> skip
			end;
		_ -> skip
	end;

%%创建长沙麻将房间
process_pt(pt_create_csmj_d009,Seq,Pt,Sid) -> 
	case db:dirty_get(ply, Sid) of
		[#ply{aid = Aid}] ->
			fun_room_center:req_create_room(Sid,Aid,Pt,Seq);
		_ -> skip
	end;
%%创建转转麻将房间
process_pt(pt_create_zzmj_d008,Seq,Pt,Sid) -> 
	case db:dirty_get(ply, Sid) of
		[#ply{aid = Aid}] ->
			fun_room_center:req_create_room(Sid,Aid,Pt,Seq);
		_ -> skip
	end;

process_pt(pt_create_jd_sg_d200,Seq,Pt,Sid) -> 
	case db:dirty_get(ply, Sid) of
		[#ply{aid = Aid}] ->
			fun_room_center:req_create_room(Sid,Aid,Pt,Seq);
		_ -> skip
	end;

process_pt(pt_create_jhsg_d202,Seq,Pt,Sid) -> 
	case db:dirty_get(ply, Sid) of
		[#ply{aid = Aid}] ->
			fun_room_center:req_create_room(Sid,Aid,Pt,Seq);
		_ -> skip
	end;

process_pt(pt_create_swsg_d201,Seq,Pt,Sid) -> 
	case db:dirty_get(ply, Sid) of
		[#ply{aid = Aid}] ->
			fun_room_center:req_create_room(Sid,Aid,Pt,Seq);
		_ -> skip
	end;

process_pt(_PtModule,_Seq,_Pt,_Sid) -> ok.


reported_ply_num()->
	case db:dirty_get(ply,#ply{_='_'}) of
		Plys when is_list(Plys) ->
			?log_trace("reported_ply_num=~p",[length(Plys)]),
			length(Plys);
		_->skip
	end.